package com.example.my_music_palyer.common.tools;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


//实现图片上传功能
@RestController
public class ImageTools {

    @Value("${image.local}")
    private String url;

    public int upload(MultipartFile file, int userId) throws IOException {
        String fileName = file.getOriginalFilename();//获取文件名
        String path = "/" + fileName;//拼接访问地址
        String savePath = url + fileName;
        System.out.println("存储地址:" + fileName + "访问地址:{}" + path);
        File saveFile = new File(savePath);

        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
        file.transferTo(saveFile);
        return 1;
    }

    public int delete(String fileName) {
        String fileUrl = url + fileName;
        File file = new File(fileUrl);
        if (file.delete()) {
            return 1;
        }
        return 0;
    }
}
